package io.gitee.caoxiaoyu97.service.split.agent;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
import org.springframework.beans.factory.support.BeanNameGenerator;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.EnvironmentAware;
import org.springframework.core.env.Environment;
import org.springframework.util.StringUtils;

/*********************************************************
 * 文件名称：ServiceSplitScannerConfigurer.java
 * 系统名称：cxycloud
 * 模块名称：io.gitee.caoxiaoyu97.service.split
 * 功能说明：处理服务拆分的配置类
 * 开发人员 @author：caoxy
 * 开发时间 @date：2023/8/4 14:59
 * 修改记录：程序版本  修改日期  修改人员  修改单号  修改说明
 *********************************************************/
public class ServiceSplitScannerConfigurer implements BeanDefinitionRegistryPostProcessor, ApplicationContextAware, EnvironmentAware {
    private static final String SERVICE_SCAN_PACKAGE_KEY = "cxycloud.service.split.basepackage";
    private static final String SERVICE_SCAN_PACKAGE_DEFAULT = "io.gitee.caoxiaoyu97.**.service.inter";
    private static final String INTERCEPTOR_NAME_KEY = "cxycloud.service.split.interceptorName";
    private static final String INTERCEPTOR_NAME_DEFAULT = "cxycloudServiceSplitWrapperInterceptor";
    private String serviceScanPackageKey;
    private String interceptorName;
    private ApplicationContext applicationContext;
    private BeanNameGenerator beanNameGenerator = new ServiceSplitSBeanNameGenerator();

    public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry beanDefinitionRegistry) throws BeansException {
        ServiceSplitBeanDefinitionScanner scanner = new ServiceSplitBeanDefinitionScanner(beanDefinitionRegistry, this.interceptorName);
        scanner.setResourceLoader(this.applicationContext);
        scanner.setBeanNameGenerator(this.beanNameGenerator);
        scanner.registerFilters();
        scanner.scan(StringUtils.tokenizeToStringArray(this.serviceScanPackageKey, ",; \t\n"));
    }

    public void postProcessBeanFactory(ConfigurableListableBeanFactory configurableListableBeanFactory) throws BeansException {
    }

    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

    public void setEnvironment(Environment environment) {
        if (environment != null) {
            this.serviceScanPackageKey = environment.getProperty(SERVICE_SCAN_PACKAGE_KEY, SERVICE_SCAN_PACKAGE_DEFAULT);
            this.interceptorName = environment.getProperty(INTERCEPTOR_NAME_KEY, INTERCEPTOR_NAME_DEFAULT);
        } else {
            this.serviceScanPackageKey = SERVICE_SCAN_PACKAGE_DEFAULT;
            this.interceptorName = INTERCEPTOR_NAME_DEFAULT;
        }

    }
}
